<template>
	<div class="app-container">
		<jk-operation-card ref="box1" class="margin-bottom-10">
			<div class="flex-between-center-wrap">
				<div class="flex-start-center-wrap">
					<jk-add-button :permission-prefix="prefix" class="margin-5-0 item-gap" @click="onAddBtn"></jk-add-button>
				</div>
				<div class="flex-end-center-wrap">
					<jk-select v-model="searchParams.auditState" :option-list="auditStateList" placeholder="状态" clearable size="small" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>
					<jk-select v-model="searchParams.workshopId" :option-list="workshopList" placeholder="区域" size="small" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>
					<jk-select v-model="searchParams.typeId" :option-list="typeList" placeholder="品种属性" size="small" class="search-bar-item-size1 margin-5-0 item-gap"></jk-select>
					<jk-search-input v-model="searchParams.nameOrCode" placeholder="编码或名称" size="small" class="search-bar-item-size2 margin-5-0 item-gap" clearable @on-enter="onSearchBtn" />
					<jk-search-button @click="onSearchBtn"></jk-search-button>
				</div>
			</div>
		</jk-operation-card>

		<jk-card>
			<jk-table :loading="tableLoading" :data="tableData" :height="tableHeight + 20">
				<vxe-column title="编码" align="left" field="code" :min-width="100" />
				<vxe-column title="名称" align="left" field="name" :min-width="100" />
				<vxe-column title="品种属性" align="left" field="typeName" :min-width="100" />
				<vxe-column title="创建人" align="left" field="creatorName" :width="90"></vxe-column>
				<vxe-column title="创建时间" align="left" field="createTime" :width="160"></vxe-column>
				<vxe-column title="修改人" align="left" field="updaterName" :width="90"></vxe-column>
				<vxe-column title="修改时间" align="left" field="updateTime" :width="160"></vxe-column>
				<vxe-column title="状态" align="center" field="auditState" :width="100">
					<template slot-scope="scope">
						<jk-common-status :status="scope.row.auditState"></jk-common-status>
					</template>
				</vxe-column>
				<vxe-column title="操作" align="center" fixed="right" :width="250">
					<template slot-scope="scope">
						<jk-operation-bar
							:scope="scope"
							api-prefix="spin/product-property"
							:permission-prefix="prefix"
							@on-detail="onDetailBtn"
							@on-edit="onEditBtn"
							@on-refresh="getListRequest"
						></jk-operation-bar>
					</template>
				</vxe-column>
			</jk-table>

			<!-- 分页组件 -->
			<footer ref="box2" class="flex-end-center margin-top-10">
				<jk-page
					:page-index="searchParams.pageNo"
					:page-sizes="searchParams.pageSizes"
					:page-size="searchParams.pageSize"
					:total="totalCount"
					@on-change="onPageChange"
				/>
			</footer>
		</jk-card>
		<detail-dialog
			:data-id="dataId"
			:dialog-state="detailDialogState"
			@on-visible-change="onVisibleChangeOfDetailDialog"
		></detail-dialog>
		<save-dialog
			:permission-prefix="prefix"
			:data-id="dataId"
			:dialog-state="saveDialogState"
			@on-visible-change="onVisibleChange"
			@on-finish="onFinish"
		></save-dialog>
	</div>
</template>

<script>
    import saveDialog from './save-dialog';
    import detailDialog from './detail-dialog';
    import calHeight from '/src/mixins/cal-table-height';
    import { pageConfig } from '/src/utils/jk/jk-tool';
    import {
        spinProductPropertyPage
    } from '/src/api/material/property';
    import { auditStateList } from '../../../utils/jk/status-option-list';
    import { systemBusinessDictPage } from '../../../api/basicData/businessDict';

    export default {
        name: 'MaterialProperty',
        components: {
            saveDialog,
            detailDialog
        },
        mixins: [calHeight],
        data() {
            return {
                apiItem: {
                    'page': spinProductPropertyPage
                },
                detailDialogState: false,
                prefix: 'personnel:group',
                auditStateList: auditStateList,
                dataId: null,
                typeList: [],
                workshopList: [],
                saveDialogState: false,
                tableLoading: true,
                tableData: [],
                totalCount: 0,
                searchParams: {
                    pageNo: pageConfig.pageNo,
                    pageSize: pageConfig.pageSize,
                    nameOrCode: '',
                    typeId: null,
                    auditState: null,
                    workshopId: null
                }
            };
        },
        methods: {
            onSearchBtn() {
                this.$set(this.searchParams, 'pageNo', 1);
                this.getListRequest();
            },
            onPageChange(e, size) {
                this.$set(this.searchParams, 'pageNo', e);
                this.$set(this.searchParams, 'pageSize', size);
                this.getListRequest();
            },
            getProductPropertyListRequest() {
                return systemBusinessDictPage({ categoryCode: 'PRODUCT_PROPERTY' }).then(res => {
                    if (res.data.status === 200) {
                        this.typeList = res.data.res;
                    }
                });
            },
            onDetailBtn(e) {
                this.dataId = e.id;
                this.detailDialogState = true;
            },
            onVisibleChangeOfDetailDialog(e) {
                this.detailDialogState = e;
                if (!e) {
                    this.dataId = null;
                }
            },
            onFinish() {
                this.saveDialogState = false;
                this.getListRequest();
            },
            onVisibleChange(e) {
                this.saveDialogState = e;
                if (!e) {
                    this.dataId = null;
                }
            },
            /** 查询列表 */
            getListRequest() {
                this.tableLoading = true;
                return this.apiItem['page'](this.searchParams).then(res => {
                    this.tableLoading = false;
                    if (res.data.status === 200) {
                        this.tableData = res.data.res;
                        this.totalCount = res.data.count;
                    }
                });
            },
            /** 新增按钮操作 */
            onAddBtn() {
                this.saveDialogState = true;
            },
            /** 修改按钮操作 */
            onEditBtn(row) {
                this.dataId = row.id;
                this.saveDialogState = true;
            },
            async getDependentData() {
                this.tableLoading = true;
                await this.getProductPropertyListRequest();
                await this.getListRequest();
                this.tableLoading = false;
            }
        },
        mounted() {
            this.workshopList = this.$store.state.user.userWorkshop.workshopList;
            if (!this.searchParams.workshopId) {
                this.$set(this.searchParams, 'workshopId', this.$store.state.user.userWorkshop.defaultWorkshopId);
            }
            this.getDependentData();
        }
    };
</script>
